#!/usr/bin/env perl

use strict;
use warnings;
use POSIX qw(strftime);
use Term::ANSIColor;


my $logFile;
if(@ARGV >= 1) {
  my $logFileName = $ARGV[0];
  open(my $fw, '>', $logFileName) or die "Could not open file '$logFileName' $!";
  $logFile = $fw;
}

sub finish {
  if (defined $logFile) {
    close $logFile;
  }
  exit;
}
$SIG{INT}  = \&finish;
$SIG{TERM} = \&finish;

while (my $inputLine = <STDIN>) {
  my $prefix = strftime("[%Y-%m-%d %H:%M:%S]", localtime);
  print colored(['yellow'], $prefix), " ", $inputLine;

  if (defined $logFile) {
    print $logFile $prefix, " ", $inputLine;
  }
}

finish();

